/*
 * Copy right (C) Ji Guo-rui
 * 14 Jun 2008 02:27:41 +0800
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "uliconfig.h"
#include "ulilog.h"


/*ulilog config*/
#define ULILOG_DEBUG_OUTPUT
#define ULILOG_ERROR_OUTPUT
#define ULILOG_INFO_OUTPUT


struct ulilog_h
{
    char filename[256];
    FILE *fp;
    long size;
};

static struct ulilog_h *m_ulilog_h;

int ulilog_open(const char * file_name, long file_size)
{
    m_ulilog_h = malloc(sizeof(struct ulilog_h));
	if(m_ulilog_h == NULL)
	{
		return (-1);
	}
    m_ulilog_h -> fp = fopen(file_name, "a");
	if(m_ulilog_h->fp == NULL)
	{
		return (-1);
	}
    strcpy(m_ulilog_h->filename, file_name);
    m_ulilog_h->size = file_size;
    return (0);
}
int ulilog_close()
{
    if(m_ulilog_h -> fp)
    {
        fclose(m_ulilog_h->fp);
        m_ulilog_h->fp = NULL;
    }
    if(m_ulilog_h)
    {
        free(m_ulilog_h);
        m_ulilog_h = NULL;
    }
    return (0);
}
#define ULILOG_COMMON_DECLARE \
    char	szLog[256];       \
	char	szLine[512];      \
    struct tm 	time;         \
    va_list argList;          \
    long	lLength
#define ULILOG_WRITE(fmt, szLog, szLine, time, argList, lLength) do{\
    va_start( argList, fmt );                                    \
    vsprintf( szLog, fmt, argList );                             \
    va_end( argList );                                           \
	if(m_ulilog_h == NULL || m_ulilog_h->fp == NULL) return (-1);\
    lLength = ftell(m_ulilog_h->fp);                             \
    if (lLength > m_ulilog_h->size)                              \
    {                                                            \
	    fclose(m_ulilog_h->fp);                                  \
	    m_ulilog_h->fp = fopen(m_ulilog_h->filename, "w");       \
		if(m_ulilog_h->fp == NULL) return (-1);                  \
    }                                                            \
    _getsystime(&time);                                          \
    sprintf(szLine, "%04d-%02d-%02d %02d:%02d:%02d \t%s\n",      \
	    1900 + time.tm_year, time.tm_mon + 1,time.tm_mday,       \
	    time.tm_hour, time.tm_min, time.tm_sec,                  \
	    szLog);                                                  \
    fputs(szLine, m_ulilog_h->fp);                               \
    fflush(m_ulilog_h->fp);                                      \
}while(0)

int ulilog_debug(const char * fmt, ...)
{
#if defined ULILOG_DEBUG_OUTPUT
    ULILOG_COMMON_DECLARE;
    ULILOG_WRITE(fmt, szLog, szLine, time, argList, lLength);
#endif
    return (0);
}

int ulilog_error(const char * fmt, ...)
{
#if defined ULILOG_ERROR_OUTPUT
    ULILOG_COMMON_DECLARE;
    ULILOG_WRITE(fmt, szLog, szLine, time, argList, lLength);
#endif
    return (0);
}

int ulilog_info(const char * fmt, ...)
{
#if defined ULILOG_INFO_OUTPUT
    ULILOG_COMMON_DECLARE;
    ULILOG_WRITE(fmt, szLog, szLine, time, argList, lLength);
#endif
    return (0);
}

#if defined ULIDEBUG
int ulilog_test()
{
	int i, ret;
    ret = ulilog_open("ulilog_test.txt", 204800);
	for(i = 0; i< 10000; i++)
	{
        ulilog_debug("debug:%s\n", "hello, ulilog");
		ulilog_error("error:%d,%d\n", 445, 4433);
		ulilog_info("info: %d, %d", 1,2);
	}
    ulilog_close();
    return (0);
}
#endif